﻿package Cain.iphoneMode
{
	import flash.display.DisplayObject;
	import flash.display.SpreadMethod;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import gs.TweenMax;
	/**
	 * 仿iphone滑動捲軸
	 * @author Cain
	 */
	public class MoveGrup extends Sprite
	{
		private const MO:Number = 0.5;//磨擦力
		private var ww:Number;
		private var hh:Number;
		private var tt:TextField;
		private var sp:Sprite;
		private var txt:String;
		private var mc:DisplayObject;
		private var d0:Number = 0;
		private var d1:Number = 0;
		private var d2:Number = 0;
		private var down_y:Number = 0;
		private var v:Number = 0;
		private var foot_y:Number;
		/**
		 * 建構一個觸控捲軸
		 * @param	_ww 寬
		 * @param	_hh 高
		 */
		public function MoveGrup(_ww:Number,_hh:Number) 
		{
			ww = _ww;
			hh = _hh;
			init();
		}
		/**
		* 跳至某座標 0.5 秒
		* @param	_y 內容的y座標,一開始是0
		*/
		public function go(_y:Number = 0):void 
		{
			try 
			{
				TweenMax.to(sp, 0.5, { y:_y } );
			}catch (err:Error)
			{
				trace("MoveGrup "+name+" 未輸入內容");
			}
		}
		private function init():void
		{
			addEventListener(Event.ADDED_TO_STAGE, addthis);
		}
		
		private function addthis(e:Event):void 
		{
			removeEventListener(Event.ADDED_TO_STAGE, addthis);
			addEventListener(Event.REMOVED_FROM_STAGE, removethis);
		}
		
		private function removethis(e:Event):void 
		{
			remove_sp();
			removeEventListener(Event.ENTER_FRAME, movein);
			removeEventListener(Event.ENTER_FRAME, playit);
			removeEventListener(Event.REMOVED_FROM_STAGE, removethis);
		}
		private function remove_sp():void 
		{
			try 
			{
				removeChild(sp)
				sp.removeEventListener(MouseEvent.MOUSE_DOWN, sp_down);
				sp.removeEventListener(MouseEvent.MOUSE_UP, sp_up);
				sp.removeEventListener(MouseEvent.MOUSE_OUT, sp_up);
			}catch (err:Error)
			{
				
			}
		}
		/**
		 * 傳入文字資料
		 * @param	_str 內容,可含hmtl
		 */
		public function setup_txt(_str:String):void 
		{
			txt = _str;
			remove_sp();//移除舊資料
			tt_setup();//內容設定
			mk_set();
			acc_setup();//動作設定
		}
		/**
		 * 傳入元件
		 * @param	_mc 內容
		 */
		public function setup(_mc:DisplayObject):void 
		{
			mc = _mc;
			remove_sp();//移除舊資料
			mc_setup();//內容設定
			mk_set();
			acc_setup();//動作設定
		}
		
		private function mc_setup():void
		{
			sp = new Sprite();
			sp.addChild(mc);
			addChild(sp);
			var bn:Sprite = new Sprite();
			bn.graphics.beginFill(0, 0);
			bn.graphics.drawRect(0, 0, ww, sp.height);
			sp.addChild(bn);
			sp.addChild(mc);
			foot_y = hh - sp.height - 50;
		}
		private function acc_setup():void
		{
			if (sp.height>hh)//如果須下拉的話就執行動作
			{
				sp.buttonMode = true;
				acc_set();
			}
		}
		
		private function acc_set():void
		{
			sp.addEventListener(MouseEvent.MOUSE_DOWN, sp_down);
			
		}
		//手指拿起
		private function sp_up(e:MouseEvent):void 
		{
			sp.removeEventListener(MouseEvent.MOUSE_UP, sp_up);
			sp.removeEventListener(MouseEvent.MOUSE_OUT, sp_up);
			sp.addEventListener(MouseEvent.MOUSE_DOWN, sp_down);
			removeEventListener(Event.ENTER_FRAME, playit);
			move();
		}
		//移動
		private function move():void
		{
			v = d1 - d2;
			//v *= 0.8;
			addEventListener(Event.ENTER_FRAME, movein);
		}
		
		private function movein(e:Event):void 
		{
			if (v==0) 
			{
				removeEventListener(Event.ENTER_FRAME, movein);
			}
			sp.y += v;
			if (v>0) 
			{
				v -= MO;
			}else {
				v += MO;
			}
			
			if (Math.abs(v)<MO) 
			{
				v = 0;
			}else if (sp.y>0){
				v = 0;
				//sp.y = 0;
				TweenMax.to(sp, 0.2, { y:0 } );
			}else if(sp.y<foot_y) 
			{
				v = 0;
				TweenMax.to(sp, 0.2, { y:foot_y } );
			}
		}
		//手指按下
		private function sp_down(e:MouseEvent):void 
		{
			sp.removeEventListener(MouseEvent.MOUSE_DOWN, sp_down);
			sp.addEventListener(MouseEvent.MOUSE_UP, sp_up);
			sp.addEventListener(MouseEvent.MOUSE_OUT, sp_up);
			d0 = mouseY;
			d1 = sp.y;
			d2 = sp.y;
			down_y = sp.y;
			v = 0;
			addEventListener(Event.ENTER_FRAME, playit);
		}
		//按下後每格執行
		private function playit(e:Event):void 
		{
			d2 = d1;
			d1 = sp.y;
			sp.y = down_y+mouseY - d0;
		}
		
		private function mk_set():void
		{
			var mk:Sprite = new Sprite();
			mk.graphics.beginFill(0);
			mk.graphics.drawRect(0, 0, ww, hh);
			mk.graphics.endFill();
			mask = mk;
		}
		
		private function tt_setup():void
		{
			
			tt = new TextField();
			tt.selectable = false;
			tt.width = ww;
			tt.multiline = true;
			tt.wordWrap = true;
			tt.autoSize = TextFieldAutoSize.CENTER;
			tt.htmlText = txt;
			sp = new Sprite();
			sp.addChild(tt);
			addChild(sp);
			var bn:Sprite = new Sprite();
			bn.graphics.beginFill(0, 0);
			bn.graphics.drawRect(0, 0, ww, sp.height);
			sp.addChild(bn);
			//sp.addChild(tt);
			foot_y = hh - sp.height - 50;
		}
	}
	
}